package com.ruoyi.system.service.impl;

import java.util.Iterator;
import java.util.List;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.DeviceTreeSelect;
import com.ruoyi.system.domain.Drtypeinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.GlobalDrTypeMode;
import com.ruoyi.system.mapper.GlobalDrTypeInfoMapper;
import com.ruoyi.system.domain.GlobalDrTypeInfo;
import com.ruoyi.system.service.IGlobalDrTypeInfoService;

/**
 * 全局设备类型Service业务层处理
 * 
 * @author yaozh
 * @date 2024-03-16
 */
@Service
public class GlobalDrTypeInfoServiceImpl implements IGlobalDrTypeInfoService 
{
    @Autowired
    private GlobalDrTypeInfoMapper globalDrTypeInfoMapper;

    /**
     * 查询全局设备类型
     * 
     * @param drtypeid 全局设备类型主键
     * @return 全局设备类型
     */
    @Override
    public GlobalDrTypeInfo selectGlobalDrTypeInfoByDrtypeid(Long drtypeid)
    {
        return globalDrTypeInfoMapper.selectGlobalDrTypeInfoByDrtypeid(drtypeid);
    }

    /**
     * 查询全局设备类型列表
     * 
     * @param globalDrTypeInfo 全局设备类型
     * @return 全局设备类型
     */
    @Override
    public List<GlobalDrTypeInfo> selectGlobalDrTypeInfoList(GlobalDrTypeInfo globalDrTypeInfo)
    {
        return globalDrTypeInfoMapper.selectGlobalDrTypeInfoList(globalDrTypeInfo);
    }

    @Override
    public List<GlobalDrTypeInfo> selectGlobalDrTypeForAll(){
        return globalDrTypeInfoMapper.selectGlobalDrTypeForAll();
    }

    @Override
    public List<DeviceTreeSelect> selectDrTypeTreeList(GlobalDrTypeInfo globalDrTypeInfo) {
        List<GlobalDrTypeInfo> drTypeList = SpringUtils.getAopProxy(this).selectGlobalDrTypeInfoList(globalDrTypeInfo);
        return buildDeptTreeSelect(drTypeList);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param drTypeList 设备类型列表
     * @return 树结构列表
     */
    @Override
    public List<GlobalDrTypeInfo> buildDeptTree(List<GlobalDrTypeInfo> drTypeList)
    {
        List<GlobalDrTypeInfo> returnList = new ArrayList<GlobalDrTypeInfo>();
        List<Long> tempList = drTypeList.stream().map(GlobalDrTypeInfo::getDrtypeid).collect(Collectors.toList());
        for (GlobalDrTypeInfo drTypeInfo : drTypeList)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(drTypeInfo.getParentId()))
            {
                recursionFn(drTypeList, drTypeInfo);
                returnList.add(drTypeInfo);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = drTypeList;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param drTypeList 设备类型列表
     * @return 下拉树结构列表
     */
    @Override
    public List<DeviceTreeSelect> buildDeptTreeSelect(List<GlobalDrTypeInfo> drTypeList)
    {
        List<GlobalDrTypeInfo> deptTrees = buildDeptTree(drTypeList);
        return deptTrees.stream().map(DeviceTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<GlobalDrTypeInfo> list, GlobalDrTypeInfo t)
    {
        // 得到子节点列表
        List<GlobalDrTypeInfo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (GlobalDrTypeInfo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<GlobalDrTypeInfo> getChildList(List<GlobalDrTypeInfo> list, GlobalDrTypeInfo t)
    {
        List<GlobalDrTypeInfo> tlist = new ArrayList<GlobalDrTypeInfo>();
        Iterator<GlobalDrTypeInfo> it = list.iterator();
        while (it.hasNext())
        {
            GlobalDrTypeInfo n = (GlobalDrTypeInfo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDrtypeid().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<GlobalDrTypeInfo> list, GlobalDrTypeInfo t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 新增全局设备类型
     * 
     * @param globalDrTypeInfo 全局设备类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGlobalDrTypeInfo(GlobalDrTypeInfo globalDrTypeInfo)
    {
        int rows = globalDrTypeInfoMapper.insertGlobalDrTypeInfo(globalDrTypeInfo);
        insertGlobalDrTypeMode(globalDrTypeInfo);
        return rows;
    }

    /**
     * 修改全局设备类型
     * 
     * @param globalDrTypeInfo 全局设备类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGlobalDrTypeInfo(GlobalDrTypeInfo globalDrTypeInfo)
    {
        globalDrTypeInfoMapper.deleteGlobalDrTypeModeByDrtypeid(globalDrTypeInfo.getDrtypeid());
        insertGlobalDrTypeMode(globalDrTypeInfo);
        return globalDrTypeInfoMapper.updateGlobalDrTypeInfo(globalDrTypeInfo);
    }

    /**
     * 批量删除全局设备类型
     * 
     * @param drtypeids 需要删除的全局设备类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGlobalDrTypeInfoByDrtypeids(Long[] drtypeids)
    {
        globalDrTypeInfoMapper.deleteGlobalDrTypeModeByDrtypeids(drtypeids);
        return globalDrTypeInfoMapper.deleteGlobalDrTypeInfoByDrtypeids(drtypeids);
    }

    /**
     * 删除全局设备类型信息
     * 
     * @param drtypeid 全局设备类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGlobalDrTypeInfoByDrtypeid(Long drtypeid)
    {
        globalDrTypeInfoMapper.deleteGlobalDrTypeModeByDrtypeid(drtypeid);
        return globalDrTypeInfoMapper.deleteGlobalDrTypeInfoByDrtypeid(drtypeid);
    }

    /**
     * 新增全局设备类型模板信息
     * 
     * @param globalDrTypeInfo 全局设备类型对象
     */
    public void insertGlobalDrTypeMode(GlobalDrTypeInfo globalDrTypeInfo)
    {
        List<GlobalDrTypeMode> globalDrTypeModeList = globalDrTypeInfo.getGlobalDrTypeModeList();
        Long drtypeid = globalDrTypeInfo.getDrtypeid();
        if (StringUtils.isNotNull(globalDrTypeModeList))
        {
            List<GlobalDrTypeMode> list = new ArrayList<GlobalDrTypeMode>();
            for (GlobalDrTypeMode globalDrTypeMode : globalDrTypeModeList)
            {
                globalDrTypeMode.setDrtypeid(drtypeid);
                list.add(globalDrTypeMode);
            }
            if (list.size() > 0)
            {
                globalDrTypeInfoMapper.batchGlobalDrTypeMode(list);
            }
        }
    }
}
